package com.example.hlddz2.ui.activity;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.example.hlddz2.R;
import com.example.hlddz2.databinding.ActivityMainBinding;
import com.example.hlddz2.ui.fragment.CategoryFragment;
import com.example.hlddz2.ui.fragment.HomeFragment;
import com.example.hlddz2.ui.fragment.CartFragment;
import com.example.hlddz2.ui.fragment.ProfileFragment;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    private HomeFragment homeFragment;
    private CategoryFragment categoryFragment;
    private CartFragment cartFragment;
    private ProfileFragment profileFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        homeFragment = new HomeFragment();
        categoryFragment = new CategoryFragment();
        profileFragment = new ProfileFragment();
        cartFragment = new CartFragment();

        // 默认加载首页
        loadFragment(homeFragment);

        binding.bottomNavigation.setOnItemSelectedListener(item -> {
            Fragment selectedFragment = null;
            if (item.getItemId() == R.id.navigation_home) {
                selectedFragment = homeFragment;
            } else if (item.getItemId() == R.id.navigation_category) {
                selectedFragment = categoryFragment;
            } else if (item.getItemId() == R.id.navigation_cart) {
                selectedFragment = cartFragment;
            } else if (item.getItemId() == R.id.navigation_profile) {
                selectedFragment = profileFragment;
            }
            if (selectedFragment != null) {
                loadFragment(selectedFragment);
            }
            return true;
        });
    }

    private void loadFragment(Fragment fragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, fragment);
        transaction.commit();
    }
}